<template>
  <div class="app-container">
    <h4 class="form-header h4">新增转岗</h4>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="margin-left:24px;">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="18" :md="12" :lg="6">
          <el-form-item label="名称" prop="employeeName">
            <el-input v-model="form.employeeName" placeholder="请输入名称" readonly>
              <el-button slot="append" icon="el-icon-search" @click="handleSearch()"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="新调部门" prop="postExchangeDeptId">
            <treeselect v-model="form.postExchangeDeptId" :options="deptOptions" :show-count="true" placeholder="请选择部门" @select="deptIdChange" />
          </el-form-item>
          <el-form-item label="原始部门" prop="deptId">
            <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择部门" disabled />
          </el-form-item>
          <el-form-item label="证件号码" prop="employeeIdCard">
            <el-input v-model="form.employeeIdCard" placeholder="请输入身份证号" disabled />
          </el-form-item>
          <el-form-item label="联系电话" prop="employeePhone">
            <el-input v-model="form.employeePhone" placeholder="请输入联系电话" disabled />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">保 存</el-button>
            <el-button @click="close()">返 回</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <!-- <div style="border:solid 1px red;">明细</div> -->
        </el-col>
      </el-row>

      <!-- 对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
        <el-row :gutter="20">
          <!--部门数据-->
          <el-col :span="6" :xs="24">
            <div class="head-container">
              <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
            </div>
            <div class="head-container">
              <el-tree :style="defaultHeight" :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
            </div>
          </el-col>
          <!--人员数据-->
          <el-col :span="18" :xs="24">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="" prop="employeeCode" label-width="40px">
                <el-input v-model="queryParams.employeeCode" placeholder="请输入警号" clearable style="width: 120px" @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="" prop="employeeName" label-width="40px">
                <el-input v-model="queryParams.employeeName" placeholder="请输入名称" clearable style="width: 120px" @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <el-table highlight-current-row v-loading="loading" :data="employeeList" @current-change="handleSelectionChange">
              <el-table-column type="index" width="36" />
              <el-table-column label="警号" align="center" prop="employeeCode" width="67" />
              <el-table-column label="名称" align="center" prop="employeeName" width="76" />
              <el-table-column label="身份证号" align="center" prop="employeeIdCard" width="161" />
              <el-table-column label="性别" align="center" prop="employeeSex" width="46">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.employeeSex" />
                </template>
              </el-table-column>
              <el-table-column label="机构" align="center" prop="employeeAgency" width="72">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.emp_employee_agency" :value="scope.row.employeeAgency" />
                </template>
              </el-table-column>
              <el-table-column label="部门" align="center" prop="employeeDepartment.deptName" :show-overflow-tooltip="true" />
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            <div style="float:right;margin-bottom:8px;margin-right:10px;position:absolute;right:0;bottom:0;">
              <el-button type="primary" size="mini" @click="submitFormDialog()">确 定</el-button>
              <el-button size="mini" @click="closeDialog()">返 回</el-button>
            </div>
          </el-col>
        </el-row>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import { deptTreeSelect } from "@/api/system/user";
import { listEmployee, getEmployee } from '@/api/employee/list';
import { addPost } from '@/api/employee/post';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: 'employee-list-add',
  dicts: ['sys_user_sex', 'emp_employee_status', "emp_employee_education", "emp_employee_politics", "emp_employee_domicile", "emp_recruit_type", "emp_recruit_batch",
    "emp_employee_agency", "emp_employee_type"],
  components: { Treeselect },
  data() {
    return {
      form: {
        employeeDepartment: { deptName: undefined, },
      },
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      employeeId: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      employeeList: null,
      // 部门名称
      deptName: undefined,
      postExchangeDeptId: undefined,
      postExchangeDeptName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 部门树状结构高度
      defaultHeight: {
        height: ''
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeCode: undefined,
        employeeName: undefined,
        employeeIdCard: undefined,
        status: "1",
        deptId: undefined
      },
      rules: {
        employeeName: [{ required: true, message: '名称不能为空', trigger: 'change' }],
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {

  },
  mounted() { },

  methods: {
    /** 查询人员列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then((response) => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // deptId变化
    deptIdChange(val) {
      this.postExchangeDeptId = val.id;
      this.postExchangeDeptName = val.label;
      this.$nextTick(() => {
        this.$refs.form.validateField('deptId');
      })
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.handleQuery();
    },
    // 框选中数据
    handleSelectionChange(val) {
      this.employeeId = val != null ? val.employeeId : undefined;
    },
    /** 新增按钮操作 */
    handleSearch() {
      this.open = true;
      this.title = "选择人员";
      this.getHeight();
      this.resetQuery();
      this.getDeptTree();
    },
    /** 提交按钮 */
    submitFormDialog() {
      const employeeId = this.employeeId;
      if (employeeId == undefined) {
        this.$message.error('请选择人员后点击确定按钮或直接点击返回按钮！');
        return
      }
      getEmployee(employeeId).then(response => {
        this.form = response.data;
        this.closeDialog();
        // 审核中与已审核禁用标识
        // if (this.form.auditStatus == 1 || this.form.auditStatus == 2) {
        //   this.disableInput = true;
        // }
        this.open = false;
      });
    },
    /** 关闭按钮 */
    closeDialog() {
      this.open = false;
    },
    getHeight() {
      this.defaultHeight.height = window.innerHeight - 330 + 'px';
    },
    /** 提交按钮 */
    submitForm() {
      // 原部门Id与新部门Id比较
      if (this.form.deptId == this.postExchangeDeptId) {
        this.$message.error('新调部门与原始部门不能相同！');
        return;
      }
      this.form.postOriginalDeptId = this.form.deptId;
      this.form.postOriginalDeptName = this.form.employeeDepartment.deptName;
      this.form.postExchangeDeptId = this.postExchangeDeptId;
      this.form.postExchangeDeptName = this.postExchangeDeptName;
      this.$refs['form'].validate(valid => {
        if (valid) {
          addPost(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.close();
          });
        }
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: '/employee/post' };
      this.$tab.closeOpenPage(obj);
    }
  }
};
</script>

<style scoped>
.el-tree {
  /* display: flex; */
  overflow: hidden;
}
.el-tree:hover {
  overflow: auto;
}
/deep/.el-dialog__body {
  padding: 4px 20px;
}
</style>